<template>
  <div class="form-container">
    <h1 class="form-title">走访信息录入表</h1>
    <form @submit.prevent="validateAndSubmit">
      <div class="form-grid">
        <div class="form-group">
          <label for="familyId" class="required">贫困户编号</label>
          <input type="number" id="familyId" v-model.number="formData.familyId" required  placeholder="请输入您的贫困户编号"/>
        </div>
        <div class="form-group">
          <label for="riskRemark">风险备注</label>
          <input type="text" id="riskRemark" v-model="formData.riskRemark"  placeholder="请输入风险备注" />
        </div>
        <div class="form-group">
          <label for="cadreId" class="required">驻村干部编号</label>
          <input type="text" id="cadreId" v-model="formData.cadreId" required  placeholder="请输入驻村干部编号"/>
        </div>
        <div class="form-group">
          <label for="visitTime" class="required">走访时间</label>
          <input type="date" id="visitTime" v-model="formData.visitTime" required placeholder="请输入走访时间" />
        </div>
        <div class="form-group">
          <label for="income" class="required">家庭人均纯收入(元)</label>
          <input type="number" id="income" v-model.number="formData.income" required placeholder="请输入家庭人均纯收入(元)" />
        </div>
        <div class="form-group">
          <label for="debt">家庭负债</label>
          <input type="decimal" id="debt" v-model.number="formData.debt"  placeholder="请输入负债(元)"/>
        </div>
      </div>
      
      <!-- 风险类型 -->
      <div class="form-group">
        <label>风险类型</label>
        <div class="risk-options-grid">
          <div class="risk-option" v-for="option in riskOptions" :key="option.value">
            <input type="checkbox" :id="option.id" v-model="formData.riskType" :value="option.value" @change="handleRiskTypeChange">
            <label :for="option.id">{{ option.label }}</label>
          </div>
          <div class="risk-option" v-if="formData.riskType.includes('其他')">
            <el-input v-model="formData.otherRisk" placeholder="请注明其他风险类型" clearable />
          </div>
        </div>
      </div>
      
      <div class="form-group">
        <label for="suggestions">意见与建议</label>
        <textarea id="suggestions" v-model="formData.suggestions"></textarea>
      </div>
      
      <div class="form-group">
        <label for="visitContent" class="required">走访详细内容</label>
        <textarea id="visitContent" v-model="formData.visitContent" required></textarea>
      </div>
      
      <div class="button-group">
        <button type="submit" class="btn-submit">确认</button>
        <button type="button" @click="resetForm">取消</button>
      </div>
    </form>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { addVisitInfo,updateVisitInfo,getVisitInfo,deleteVisitInfo } from '@/api/visitinfo';
import axios from 'axios';
import { ElMessage } from 'element-plus';

const formData = reactive({
  familyNumber: '',
  riskRemark: '',
  cadreNumber: '',
  income: '',
  debt: '',
  visitTime: '',
  riskType: [],
  suggestions: '',
  visitContent: ''
});

const riskOptions = ref([
  { id: 'illness', value: '因病', label: '因病' },
  { id: 'study', value: '因学', label: '因学' },
  { id: 'disability', value: '因残', label: '因残' },
  { id: 'naturalDisaster', value: '因自然灾害', label: '因自然灾害' },
  { id: 'accident', value: '因意外事故', label: '因意外事故' },
  { id: 'projectFailure', value: '因产业项目失败', label: '因产业项目失败' },
  { id: 'employmentInstability', value: '因务工就业不稳', label: '因务工就业不稳' },
  { id: 'lackLabor', value: '缺劳动力', label: '缺劳动力' },
  { id: 'other', value: '其他', label: '其他' }
]);

const router = useRouter();

const validateAndSubmit = () => {
  // 表单原生验证
  const form = document.querySelector('form');
  if (!form.checkValidity()) {
    form.reportValidity();
    return;
  }
  
  // 检查风险类型是否至少选择了一项
  if (formData.riskType.length === 0) {
    alert('请至少选择一项风险类型');
    return;
  }
  
  // 检查其他风险类型是否填写
  if (formData.riskType.includes('其他') && !formData.otherRisk.trim()) {
    alert('请注明其他风险类型');
    return;
  }

  const riskTypeStr=formData.riskType.join(',');
  const requestData={
    ...formData,
    riskType:riskTypeStr
  };
  
  // 提交表单
addVisitInfo(requestData).then(res=>{
  if(res.code==2000){
    ElMessage.success('表单提交成功');
    router.push('/interviewList');
  }else{
    ElMessage.error('提交失败，请重试');
  }
})
  
};

const resetForm = () => {
  // 重置表单数据
  Object.assign(formData, {
    povertyId: '',
    riskRemark: '',
    villageCadreId: '',
    familyIncome: '',
    familyDebt: '',
    visitTime: '',
    riskType: [],
    otherRisk: '',
    opinions: '',
    visitContent: ''
  });
  
  // 重置表单DOM
  document.querySelector('form')?.reset();
};

const handleRiskTypeChange = () => {
  // 当取消选择"其他"时，清空其他风险类型输入框
  if (!formData.riskType.includes('其他')) {
    formData.otherRisk = '';
  }
};
</script>

<style scoped>
/* 样式部分保持不变 */
.form-container {
  background-color: white;
  padding: 30px;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  max-width: 800px;
  margin: 20px auto;
}

.form-title {
  text-align: center;
  margin-bottom: 25px;
  color: #1f2937;
  font-size: 28px;
  font-weight: 600;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
  margin-bottom: 20px;
}

.form-group {
  margin-bottom: 0;
}

label {
  display: block;
  margin-bottom: 6px;
  font-weight: 500;
  color: #374151;
  font-size: 14px;
}

.required::after {
  content: ' *';
  color: #ef4444;
  font-size: 12px;
}

input,
textarea {
  width: 100%;
  padding: 10px;
  box-sizing: border-box;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  font-size: 14px;
  transition: border-color 0.2s, box-shadow 0.2s;
}

input:focus,
textarea:focus {
  outline: none;
  border-color: #3b82f6;
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

.risk-options-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin-top: 10px;
}

.risk-option {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  border-radius: 4px;
  transition: background-color 0.2s;
}

.risk-option:hover {
  background-color: #f3f4f6;
}

.risk-option input[type="checkbox"] {
  width: 18px;
  height: 18px;
  margin: 0;
}

.button-group {
  display: flex;
  justify-content: center;
  gap: 15px;
  margin-top: 25px;
}

.btn-submit {
  background-color: #3b82f6;
  color: white;
  padding: 10px 20px;
  font-size: 16px;
  border-radius: 4px;
  transition: background-color 0.2s;
}

.btn-submit:hover {
  background-color: #2563eb;
}

button[type="button"] {
  background-color: #f9fafb;
  color: #4b5563;
  border: 1px solid #d1d5db;
  padding: 10px 20px;
  font-size: 16px;
  border-radius: 4px;
  transition: background-color 0.2s;
}

button[type="button"]:hover {
  background-color: #f3f4f6;
}

/* 响应式布局 */
@media (max-width: 600px) {
  .form-grid {
    grid-template-columns: 1fr;
  }
  
  .risk-options-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .form-container {
    padding: 20px;
  }
  
  .form-title {
    font-size: 24px;
  }
}
</style>